<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/userStore'
import { ElMessage, ElLoading } from 'element-plus'
import { User, Lock, ArrowRight } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

// 表单引用
const loginFormRef = ref(null)

// 登录处理
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    // 表单验证
    await loginFormRef.value.validate()
    
    // 显示加载状态
    const loading = ElLoading.service({
      lock: true,
      text: '登录中...',
      background: 'rgba(0, 0, 0, 0.7)'
    })
    
    // 调用登录方法
    const success = await userStore.login(loginForm.username, loginForm.password)
    
    // 关闭加载状态
    loading.close()
    
    // 如果登录成功，路由跳转会在store中处理
    if (!success) {
      ElMessage.error('登录失败，请检查用户名和密码')
    }
  } catch (error) {
    console.error('登录表单验证失败:', error)
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-left">
      <h1 class="welcome-title">欢迎登录</h1>
      <h2 class="system-title">成体通体测数据上报系统</h2>
      <div class="logo-container">
        <img src="@/assets/跑步.svg" alt="体测图标" class="runner-icon" />
      </div>
    </div>
    
    <div class="login-right">
      <div class="login-form">
        <h2 class="login-heading">登 录</h2>
        <p class="login-subheading">请在下方填写您的登录信息</p>
        
        <el-form 
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          label-position="top"
        >
          <el-form-item prop="username">
            <div class="form-label">用户名</div>
              <el-input 
                v-model="loginForm.username" 
              placeholder="username"
              :prefix-icon="User"
                clearable
              />
          </el-form-item>
          
          <el-form-item prop="password">
            <div class="form-label">密码</div>
              <el-input 
                v-model="loginForm.password" 
                type="password" 
              placeholder="password"
              :prefix-icon="Lock"
                show-password
                @keyup.enter="handleLogin"
              />
          </el-form-item>
          
          <el-button 
            type="primary" 
            class="login-button" 
            @click="handleLogin"
          >
            登录
            <el-icon class="el-icon--right"><ArrowRight /></el-icon>
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  width: 100%;
}

.login-left {
  width: 50%;
  background-color: #e8eaed;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
}

.welcome-title {
  font-size: 2.5rem;
  font-weight: bold;
  margin-bottom: 0.5rem;
  color: #333;
}

.system-title {
  font-size: 1.8rem;
  font-weight: normal;
  margin-bottom: 3rem;
  color: #333;
}

.logo-container {
  margin-top: 2rem;
}

.runner-icon {
  width: 120px;
  height: auto;
  opacity: 0.7;
}

.login-right {
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 2rem;
  background-color: white;
}

.login-form {
  width: 100%;
  max-width: 450px;
  padding: 0 20px;
}

.login-heading {
  font-size: 2rem;
  font-weight: bold;
  margin-bottom: 0.5rem;
  color: #333;
  text-align: left;
}

.login-subheading {
  color: #666;
  margin-bottom: 2rem;
  text-align: left;
}

.form-label {
  display: block;
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
  font-size: 16px;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

:deep(.el-form-item__error) {
  padding-top: 4px;
}

:deep(.el-input__wrapper) {
  height: 46px;
  width: 100%;
}

:deep(.el-input) {
  width: 100%;
}

.login-button {
  width: 100%;
  margin-top: 20px;
  height: 46px;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  padding: 12px 20px;
}
</style> 